/**
 * 
 */
package com.kelaite.procurement.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.kelaite.procurement.common.CommonResponseDto;
import com.kelaite.procurement.common.RetCodeEnum;
import com.kelaite.procurement.constants.MessageTypeEnum;
import com.kelaite.procurement.dto.UserDTO;
import com.kelaite.procurement.entity.TCompany;
import com.kelaite.procurement.entity.TMessage;
import com.kelaite.procurement.entity.TPurchaseOrder;
import com.kelaite.procurement.entity.TUser;
import com.kelaite.procurement.entity.TVendor;
import com.kelaite.procurement.entity.TVendorHistory;
import com.kelaite.procurement.entity.TVendorUser;
import com.kelaite.procurement.restful.BaseController;
import com.kelaite.procurement.service.CompanyService;
import com.kelaite.procurement.service.MessageService;
import com.kelaite.procurement.service.PurchaseOrderService;
import com.kelaite.procurement.service.UserService;
import com.kelaite.procurement.service.VendorService;
import com.kelaite.procurement.service.VendorUserService;
import com.kelaite.procurement.utils.CurrentUserUtils;
import com.kelaite.procurement.utils.JsonUtil;
import com.kelaite.procurement.utils.SendMsgWebchinese;

/**
 * @author yuanchangwei
 *
 */
@Controller
@RequestMapping("/estimate")
public class EstimateController extends BaseController {

	@Autowired
	private UserService userService;

	@Autowired
	private VendorService vendorService;

	@Autowired
	private PurchaseOrderService purchaseOrderService;

	@Autowired
	private CompanyService companyService;

	@Autowired
	private MessageService messageService;
	
	@Autowired
	private VendorUserService vendorUserService;

	@RequestMapping(value = "/index", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String index(Model model, HttpServletRequest request) {

		// TODO
		UserDTO userDTO = CurrentUserUtils.getInstance().getUser();
	    TUser currentUser = this.userService.getObject(userDTO.getId());
	    
	    List<TVendorUser> vendorUserList = vendorUserService.getByProperty("tUser", currentUser);
	    TVendor vendor = null;
	    if(null!=vendorUserList.get(0)){
	    	vendor=vendorUserList.get(0).gettVendor();
	    }else{
	    	vendor = new TVendor();
	    }
	    
		List<TVendor> vendorList = this.vendorService.getByProperty("id", vendor.getId());

		List<TPurchaseOrder> tPurchaseOrder = this.purchaseOrderService
				.getByProperty("tVendor", vendor);
		for(int i = 0 ; i<tPurchaseOrder.size() ;i++){
			if(null ==tPurchaseOrder.get(i).getEvaluationScore1()){
				tPurchaseOrder.get(i).setEvaluationScore1(0.00);
			}
			if(null ==tPurchaseOrder.get(i).getEvaluationScore2()){
				tPurchaseOrder.get(i).setEvaluationScore2(0.00);
			}
			if(null ==tPurchaseOrder.get(i).getEvaluationScore3()){
				tPurchaseOrder.get(i).setEvaluationScore3(0.00);
			}
			if(null ==tPurchaseOrder.get(i).getEvaluationScore4()){
				tPurchaseOrder.get(i).setEvaluationScore4(0.00);
			}
			if(null ==tPurchaseOrder.get(i).getEvaluationScore5()){
				tPurchaseOrder.get(i).setEvaluationScore5(0.00);
			}
			
			
		}
		model.addAttribute("purchaseOrderList", tPurchaseOrder);
		model.addAttribute("vendor", vendor);
		
		 Integer thisCompanyScore =  0 ;
		 Integer mingCi =  1 ;
		 for(int i= 0 ;i<vendorList.size();i++){
			 for (int j = 0 ; j<vendor.gettVendorHistory().size(); j++){
				 thisCompanyScore = thisCompanyScore+ vendor.gettVendorHistory().get(j).getEvaluationScore();
			 }
			 
		 }
		 List<TVendor> vendorListAll = this.vendorService.getAll();
		 
		 for (int i = 0 ; i<vendorListAll.size() ; i++){
			 TVendor vendorOnly = vendorListAll.get(i);
			 List<TVendorHistory> vendorHistoryOnly = vendorOnly.gettVendorHistory();
			 Integer otherCompanyScore = 0 ;
				 for(int j=0 ;j<vendorHistoryOnly.size();j++){
					 otherCompanyScore = otherCompanyScore +vendorHistoryOnly.get(j).getEvaluationScore();
				 }
		     
			if(otherCompanyScore>thisCompanyScore){
				mingCi= mingCi+1;
			}
			 
		 }
		 Integer scoreEvl  = 0 ;
		 Integer gradeEvl  = 0 ;
		 
		 if(vendorList.size()!=0){
			 scoreEvl = thisCompanyScore / (vendorList.size());
		 }
		 List<TCompany> comPanyAll = this.companyService.getByProperty("businessType", (short)1);
		 gradeEvl = mingCi / comPanyAll.size(); 
			
			model.addAttribute("scoreEvl", scoreEvl);
			model.addAttribute("gradeEvl", gradeEvl);
		
		return "estimate/estimate_index";
	}

	@RequestMapping(value = "/list", method = { RequestMethod.GET,
			RequestMethod.POST })
	public String list() {

		return "estimate/list";
	}

	/**
	 * B41 ����������
	 * 
	 * @param model
	 * @param request
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/updateSendMessage", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public String updateSendMessage(Model model, HttpServletRequest request) {

		try {
			UserDTO userDTO = CurrentUserUtils.getInstance().getUser();

			String receiveUserId = request.getParameter("receiveUserId");
			String receiveEmail = request.getParameter("receiveEmail");
			String msgContent = request.getParameter("msgContent");

			TMessage tMessage = new TMessage();
			tMessage.setMsgType(MessageTypeEnum.CLAIM.getVal());
			tMessage.setMsgContent(msgContent);
			Long parentId = 0L;
			tMessage.setParentId(parentId);
			tMessage.setIsDeleted(0);
			tMessage.setReadStatus(0);
			tMessage.setGmtCreateTime(new Date());
			tMessage.setGmtUpdateTime(new Date());
			tMessage.setCreatedBy(userDTO.getUsername());
			tMessage.setUpdatedBy(userDTO.getUsername());

			TUser sendUser = new TUser();
			sendUser.setId(userDTO.getId());
			tMessage.settSender(sendUser);

			TUser receUser = new TUser();
			receUser.setId(Long.parseLong(receiveUserId));
			tMessage.settReceiver(receUser);

			this.messageService.save(tMessage);
			return JsonUtil
					.serialize(new CommonResponseDto<>(RetCodeEnum.SUCCESS
							.getCode(), RetCodeEnum.SUCCESS.getMsg()));
		} catch (Exception e) {
			return JsonUtil.serialize(new CommonResponseDto<String>(
					RetCodeEnum.FAIL.getCode(), e.getMessage()));
		}
	}

	@SuppressWarnings("unused")
	@RequestMapping(value = "/updateSendMsg", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
	public String updateSendMsg(Model model, HttpServletRequest request) {
		
		try
		{
			String mobilePhone = request.getParameter("mobilePhone");
			String msgContent = request.getParameter("msgContent");
			
			SendMsgWebchinese.sendMessage(mobilePhone, msgContent);
			return JsonUtil
					.serialize(new CommonResponseDto<>(RetCodeEnum.SUCCESS
							.getCode(), RetCodeEnum.SUCCESS.getMsg()));
		
		}catch (Exception e) {
			return JsonUtil.serialize(new CommonResponseDto<String>(
					RetCodeEnum.FAIL.getCode(), e.getMessage()));
		}
	}
	
	@Override
	protected Class<?> getCurrentClass() {

		return EstimateController.class;
	}
}
